package com.ourhours.message.service.wrapper;

import com.alibaba.fastjson.JSONObject;
import com.ourhours.message.Alarm.AlarmTemplate;
import com.ourhours.message.domain.SmsSend;
import com.ourhours.message.domain.Supplier;
import com.ourhours.message.enums.MessageTypeEnum;
import com.ourhours.message.enums.SupplierTypeEnum;
import com.ourhours.message.rpc.reference.huaxin.dto.MessageDto;
import com.ourhours.message.rpc.reference.huaxin.HuaxinRpc;
import com.ourhours.message.service.SmsSendService;
import com.ourhours.message.service.SupplierService;
import org.apache.http.client.HttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

@Service
@EnableAsync
public class SmsWrapper {

    private static final Logger LOGGER = LoggerFactory.getLogger(SmsWrapper.class);

    private static HttpClient httpclient;

    @Autowired
    SupplierService supplierService;

    @Autowired
    SmsSendService smsSendService;

    @Autowired
    SupplierWrapper supplierWrapper;

    @Autowired
    HuaxinRpc huaxinRpc;

    public boolean send(String phone,Long vendorId,String content) throws Exception {
        //从数据库获取运营商信息
        Supplier supplier = supplierWrapper.findSupplierWithCache(vendorId,MessageTypeEnum.SMS.getCode());
        if(supplier==null){
            LOGGER.error("SmsWrapper.send获取营运商信息失败,参数:{},结果:{}",vendorId,supplier);
            return  false;
        }

        switch (SupplierTypeEnum.fromCode(supplier.getSupplierType())){
            case SMS_HUAXIN :
                //调用华信
                JSONObject jsonObject = JSONObject.parseObject(supplier.getOuterExtraProperty());
                String url = jsonObject.getString("url");
                huaxinRpc.send(url,supplier.getOuterAppKey(),supplier.getOuterSecret(),phone,content);
                break;
            default:
        }
        return true;
    }

    /**
     * 发送报警短信，有账号后单独配置一个vendorID
     * @param vendorId
     */
    public void smsAlarmInfo(Long vendorId){
        SmsSend smsSendAlarm = new SmsSend();
        smsSendAlarm.setVendorId(vendorId);
        smsSendAlarm.setContent(AlarmTemplate.content);
        smsSendAlarm.setPhone(AlarmTemplate.phone);
        smsSendService.sendMessage(smsSendAlarm);
    }





}
